package com.wavercaz.freeforum.controllers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wavercaz.freeforum.pojo.CommonSearchParam;
import com.wavercaz.freeforum.pojo.UserInfo;
import com.wavercaz.freeforum.services.UserInfoService;
import com.wavercaz.freeforum.utils.JWTUtil;
import com.wavercaz.freeforum.utils.QueryUtil;
import com.wavercaz.freeforum.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    // 增
    @PutMapping("/userInfo")
    public ResponseResult save(@RequestBody UserInfo userInfo) {
        boolean result = userInfoService.save(userInfo);
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "新增失败");
        else return new ResponseResult(HttpStatus.OK.value(), "新增成功");
    }

    // 删
    @DeleteMapping("/userInfo")
    public ResponseResult delete(@RequestBody List<Integer> ids) {
        boolean result = userInfoService.removeByIds(ids);
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "删除失败");
        else return new ResponseResult(HttpStatus.OK.value(), "删除成功");
    }

    // 改
    @PostMapping("/userInfo")
    public ResponseResult update(@RequestBody UserInfo userInfo) {
        boolean result = userInfoService.updateById(userInfo);
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "修改失败");
        else return new ResponseResult(HttpStatus.OK.value(), "修改成功");
    }

    // 查
    @GetMapping("/userInfo")
    public ResponseResult get(@RequestHeader("Authorization") String token) {
        UserInfo userInfo = userInfoService.getById(Integer.parseInt(JWTUtil.verifyToken(token).getClaim("id").asString()));
        if(userInfo == null) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "无此相关信息");
        else return new ResponseResult(HttpStatus.OK.value(), userInfo);
    }

    // 列表
    @PostMapping("/userInfoList")
    public ResponseResult list(@RequestBody(required = false) CommonSearchParam commonSearchParam) {
        Object data = null;
        if(commonSearchParam != null) {
            QueryWrapper queryWrapper = QueryUtil.getQueryWrapper(commonSearchParam.getSearches());
            if(commonSearchParam.getPageNum()!=null && commonSearchParam.getPageSize()!=null)
                data= userInfoService.page(new Page<>(commonSearchParam.getPageNum(), commonSearchParam.getPageSize()), queryWrapper);
            else data= userInfoService.list(queryWrapper);
        }
        else data= userInfoService.list();
        System.out.println(data);
        return new ResponseResult(HttpStatus.OK.value(), data);
    }

    // vo列表
    @PostMapping("/userInfoVoList")
    public ResponseResult listVo(@RequestBody(required = false) CommonSearchParam commonSearchParam) {
        Object data = null;
        data = userInfoService.getUserInfoVoList(commonSearchParam, new Page<>(commonSearchParam.getPageNum(), commonSearchParam.getPageSize()));
        return new ResponseResult(HttpStatus.OK.value(), data);
    }
}
